package com.yeshine.opendata.controller.admin;

import com.yeshine.opendata.controller.front.AbstractFrontControllerBase;
import com.yeshine.opendata.entity.App;
import com.yeshine.opendata.entity.AppTheme;
import com.yeshine.opendata.entity.SysDictionary;
import com.yeshine.opendata.service.IAppService;
import com.yeshine.opendata.service.IAppThemeService;
import com.yeshine.opendata.service.IDictionaryService;
import com.yeshine.opendata.utils.PageResult;
import com.yeshine.opendata.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;

/**
 * Created by taohj on 2014/8/1.
 */
@Controller
@RequestMapping("/admin/app")
public class AdminAppController extends AbstractFrontControllerBase {

    @Autowired
    private IAppService service;

    @Autowired
    private IAppThemeService themeService;

    @Autowired
    private IDictionaryService dictionaryService;


    @RequestMapping("listV")
    public String listV() {
        return "admin/app/list";
    }


    @ResponseBody
    @RequestMapping("list")
    public String list(
            @RequestParam(value = "page", required = false) Integer page,
            @RequestParam(value = "rows", required = false) Integer size,
            @RequestParam(value = "id", required = false) Integer id,
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "tags", required = false) String tags,
            @RequestParam(value = "storage", required = false) Integer storage,
            @RequestParam(value = "visibility", required = false) Integer visibility,
            @RequestParam(value = "theme", required = false) Integer theme,
            @RequestParam(value = "platform", required = false) Integer platform
    ) {
        App app = new App();

        if (StringUtils.hasLength(name)) {
            app.setName(name);
        }
        if (theme != null) {
            app.setTheme(new AppTheme(theme));
        }
        app.setPlatform(new SysDictionary(platform));
        app.setStorage(new SysDictionary(storage));
        app.setVisibility(new SysDictionary(visibility));
        app.setId(id);
        app.setTags(tags);

        PageResult<App> pageResult = new PageResult<App>(page, size);

        pageResult.setT(app);
        Result<App> result = service.getByPage(pageResult);
        return result.getPageResult().toString();
    }


    @RequestMapping("createV")
    public String createV(
    ) {
        return "admin/app/create";
    }


    @ResponseBody
    @RequestMapping("create")
    public String create(
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "url", required = false) String url,
            @RequestParam(value = "theme", required = false) Integer theme,
            @RequestParam(value = "platform", required = false) Integer platform,
            @RequestParam(value = "description", required = false) String description,
            @RequestParam(value = "storage", required = false) Integer storage,
            @RequestParam(value = "tags", required = false) String tags,
            @RequestParam(value = "visibility", required = false) Integer visibility,
            @RequestParam(value = "file", required = false) MultipartFile file,
            @RequestParam(value = "image", required = false) MultipartFile image,
            @RequestParam(value = "developer", required = false) String developer,
            @RequestParam(value = "developerPhone", required = false) String developerPhone,
            @RequestParam(value = "version", required = false) String version,
            @RequestParam(value = "linkdata", required = false) String linkdata
    ) {
        App app = new App();
        app.setName(name);
        app.setUrl(url);
        app.setTheme(new AppTheme(theme));
        app.setPlatform(new SysDictionary(platform));
        app.setDescription(description);
        app.setStorage(new SysDictionary(storage));
        app.setVisibility(new SysDictionary(visibility));
        app.setTags(tags);
        app.setFile(file);
        app.setImageFile(image);
        app.setDeveloper(developer);
        app.setDeveloperPhone(developerPhone);
        app.setVersion(version);
        app.setLinkdata(linkdata);



        app.setCreater(getCurrentUser());
        app.setCreateTime(new Date());
        Result<App> result = service.create(app, getRequest());
        return result.toString();
    }

    @RequestMapping("modifyV")
    public String modifyV(
            @RequestParam("id") Integer id
    ) {
        App app = service.getById(id).getTarget();
        setRequestAttr("app", app);
        return "admin/app/modify";
    }


    @ResponseBody
    @RequestMapping("modify")
    public String modify(
            @RequestParam("id") Integer id,
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "url", required = false) String url,
            @RequestParam(value = "theme", required = false) Integer theme,
            @RequestParam(value = "platform", required = false) Integer platform,
            @RequestParam(value = "description", required = false) String description,
            @RequestParam(value = "storage", required = false) Integer storage,
            @RequestParam(value = "tags", required = false) String tags,
            @RequestParam(value = "visibility", required = false) Integer visibility,
            @RequestParam(value = "file", required = false) MultipartFile file,
            @RequestParam(value = "image", required = false) MultipartFile image,
            @RequestParam(value = "developer", required = false) String developer,
            @RequestParam(value = "developerPhone", required = false) String developerPhone,
            @RequestParam(value = "version", required = false) String version,
            @RequestParam(value = "linkdata", required = false) String linkdata
    ) {

        App app = service.getById(id).getTarget();
        app.setName(name);
        app.setUrl(url);
        app.setTheme(new AppTheme(theme));
        app.setPlatform(new SysDictionary(platform));
        app.setDescription(description);
        app.setStorage(new SysDictionary(storage));
        app.setVisibility(new SysDictionary(visibility));
        app.setTags(tags);
        app.setFile(file);
        app.setImageFile(image);
        app.setDeveloper(developer);
        app.setDeveloperPhone(developerPhone);
        app.setVersion(version);
        app.setLinkdata(linkdata);
        app.setUpdater(getCurrentUser());
        app.setUpdateTime(new Date());
        app.setFile(file);
        app.setImageFile(image);
        Result<App> result = service.modify(app, getRequest());
        return result.toString();
    }


    @ResponseBody
    @RequestMapping("remove")
    public String remove(@RequestParam("id") Integer id) {
        Result<App> result = service.remove(new App(id));
        return result.toString();
    }

    @ResponseBody
    @RequestMapping("audit")
    public  String audit(
            @RequestParam("apps[]") Integer  apps [],
            @RequestParam("auditStatus") Integer auditStatus
    ){
        Result<App> result=service.audit(auditStatus,apps);
        return  result.toString();
    }

}
